﻿CREATE  TRIGGER [dbo].[tri_dtPSP_calchrccrt] ON [dbo].[dtPSP] FOR INSERT
AS
DECLARE @numrows AS int
SET @numrows = @@rowcount
IF @numrows > 1
BEGIN
  RAISERROR('Only single row inserts are supported!', 16, 1)
  ROLLBACK TRAN
END
ELSE
IF @numrows = 1
BEGIN
  UPDATE E
  SET hrclvl =
        CASE
          WHEN E.mgrid IS NULL THEN 0
          ELSE M.hrclvl + 1
        END,
      hrccrt =
        CASE
          WHEN E.mgrid IS NULL THEN '.' 
          ELSE M.hrccrt
        END + CAST(E.CodeID AS varchar(10)) + '.',
  
hccrt =
        CASE
          WHEN E.hCode IS NULL THEN '.' 
          ELSE M.hccrt
        END + isnull(E.hCode,'') + '.'

FROM dtPSP AS E JOIN inserted AS I
    ON I.CodeID = E.CodeID
    LEFT OUTER JOIN dtPSP AS M
    ON E.mgrid = M.CodeID
END

/*
BEGIN
  RAISERROR('Only single row inserts are supported!', 16, 1)
  ROLLBACK TRAN
END
ELSE
IF @numrows = 1
BEGIN
  UPDATE E
  SET hrclvl =
        CASE
          WHEN E.mgrid IS NULL THEN 0
          ELSE M.hrclvl + 1
        END,
      hrccrt =
        CASE
          WHEN E.mgrid IS NULL THEN '.' 
          ELSE M.hrccrt
        END + CAST(E.CodeID AS varchar(10)) + '.'
  FROM dtPSP AS E JOIN inserted AS I
    ON I.CodeID = E.CodeID
    LEFT OUTER JOIN dtPSP AS M
    ON E.mgrid = M.CodeID
END


*/


